<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>课程安排</title>
    <link href="../../static/css/bootstrap.min.css" rel="stylesheet" th:href="@{/static/css/bootstrap.min.css}">
    <link href="../../static/css/dashboard.css" rel="stylesheet" th:href="@{/static/css/dashboard.css}">
</head>
<body>
<div th:insert="admin/common::topbar"></div>
<div class="container-fluid">
    <div class="row">
        <!--侧边栏-->
        <div th:insert="~{admin/common::sidebar(active='courseArrange.html')}">
        </div>
        <!--"-->
        <main class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4" role="main">
            <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
                <h3>安排课程</h3>
                <div class="btn-toolbar mb-2 mb-md-0">
                    <div class="btn-group mr-2">
                        <form id="form" method="post" th:action="@{/admin/toArriveCourseArrange}">
                            <!--onblur 失去焦点产生事件-->
                            <div class="input-group">
                                <select class="form-control" id="day_id" name="day_id">
                                    <option th:each="day:${weekList}" th:selected="${date.getDay_id()==day.getDay_id()}"
                                            th:text="${day.getWeek()}" th:value="${day.getDay_id()}">
                                    </option>
                                </select>
                                <select class="form-control input-group-addon " id="classId" name="classId">
                                    <option th:each="info:${classInfoList}"
                                            th:selected="${classInfo.getClassid()==info.getClassid()}"
                                            th:text="${info.getClass_name()}"
                                            th:value="${info.getClassid()}"></option>
                                </select>
                                <input class="input-group-addon btn btn-sm btn-primary" type="submit" value="搜索">
                            </div>
                        </form>
                    </div>
                    <div class="btn-group mr-2">
                        <button class="btn btn-primary btn-sm btn-outline-secondary" id="btn-admin">管理员</button>
                        <button class="btn btn-sm btn-outline-secondary" id="btn-student">学生端</button>
                    </div>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table table-striped table-sm">
                    <thead>
                    <tr>
                        <th>课程</th>
                        <th>授课老师</th>
                        <th>课程类型</th>
                        <th>选修/必修</th>
                        <th>授课次数</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr class="myList" th:each="teacher,index:${teacherList}">
                        <th th:text="${teacher.getSubject()}"></th>
                        <th th:text="${teacher.getTname()}"></th>
                        <th th:text="${teacher.getCourse_type()}"></th>
                        <th th:text="${teacher.getClasses()}"></th>
                        <th id="number" th:text="${teacher.getSign()==0?'还未排课':teacher.getSign()+'节课'}"
                            th:value="${teacher.getSign()}"></th>
                        <th>
                            <button class="btn btn-success" id="btn-confirm" th:value="${teacher.getSign()}">确认</button>
                        </th>
                    </tr>
                    </tbody>
                </table>
            </div>
        </main>
    </div>
</div>
<script src="../../static/js/jquery-3.6.0.min.js" th:src="@{/static/js/jquery-3.6.0.min.js}"
        type="text/javascript"></script>
<script src="../../static/js/bootstrap.min.js" th:src="@{/static/js/bootstrap.min.js}" type="text/javascript"></script>
<script src="../../static/js/btn.js" th:src="@{/static/js/btn.js}"></script>
<script>
    let day_id = [[${date.getDay_id()}]];
    let classId = [[${classInfo.getClassid()}]];
    let lesson_id = [[${classCourse.getLesson_id()}]];
    //let subject = $('#subject').text();
    //let number = $('#number').val();怀疑在表格中id必须唯一,所以导致无法正常访问
    $(function () {
        $('.myList').each(function () {
            //获取表格的按钮
            let btn = $(this).children().eq(5).children();
            let subject = $(this).children().eq(0).text();
            btn.click(function () {
                let number = btn.val();
                let judge = confirm("确定安排本节课的课程?");
                if (judge === true) {
                    if (number >= 3) {
                        alert("今天已经排了很多这门课呢!请让老师休息一下");
                        $(location).attr('href', '/admin/arrangeCourse/' + classId + '/' + day_id + '/' + lesson_id);
                    } else {
                        $.get({
                            url: "/admin/ajax/arrangeCourse",
                            data: {"classId": classId, "day_id": day_id, "lesson_id": lesson_id, "subject": subject},
                            //async: false, method: 'get',
                            success: function (result) {
                                if (result === true) {
                                    alert("排课成功!");
                                    $(location).attr('href', '/admin/courseArrange/' + classId + '/' + day_id);
                                } else {
                                    alert("这个老师已经在这个时间段被安排课程");
                                    $(location).attr('href', '/admin/courseArrange/' + classId + '/' + day_id);
                                }
                            },
                            error: function (result) {
                                alert('程序出现问题!');
                            }

                        })
                    }

                }
            })


        })
    })


</script>
</body>
</html>